package com.pdsu.graduation_design.views;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.OrderListViewAdapter;
import com.pdsu.graduation_design.adapter.PayOrderSelectTimeAdapter;
import com.pdsu.graduation_design.dto.PayCommodityDTO;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;


public class PayOrderActivity extends AppCompatActivity {


    private List<PayCommodityDTO> payCommodityDTOS;

    private int type_http;
    private ListView orderCommodityListView;
   //选择配送时间
    private LinearLayout select_time_ll;
    private Context context;
   //修改当前页面时间
    private TextView order_time_tv,all_pr_tv,yingzhifu_tv,daizhifu_tv;

    private LinearLayout submit_order_ll;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_order);


        init();
        getData();
        bind();

    }

    void init(){
        type_http = getIntent().getIntExtra("type_http",-1);
        all_pr_tv=findViewById(R.id.pay_money_all_price);
        yingzhifu_tv=findViewById(R.id.pay_money_all_yingzhifu);
        daizhifu_tv=findViewById(R.id.pay_money_all_daizhifu);
        orderCommodityListView=findViewById(R.id.order_listview);
        select_time_ll=findViewById(R.id.pay_order_select_time);
        context=this;
        order_time_tv=select_time_ll.findViewById(R.id.pay_order_time_tv);
        submit_order_ll=findViewById(R.id.pay_order_submit_ll);

    }

    void bind(){

        orderCommodityListView.setAdapter(new OrderListViewAdapter(getApplicationContext(),payCommodityDTOS));
        //选择时间监听
        select_time_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                BottomSheetDialog dialog = new BottomSheetDialog(context);
                System.out.println(context);
                View view = getLayoutInflater().inflate(R.layout.bottomsheetdialog_selecttime, null);

                String[] strings={"上午","中午","下午"};
                ListView times=view.findViewById(R.id.bottom_sheet_time_list);
                times.setAdapter(new PayOrderSelectTimeAdapter(context,strings));

                times.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        TextView title_time=view.findViewById(R.id.pay_order_select_time_textView);
                        String selected_time=title_time.getText().toString();

                        order_time_tv.setText(selected_time);
                        dialog.dismiss();
                        //

                    }
                });
                //给时间表设置
                dialog.setContentView(view);

                try {

                    dialog.show();
                }catch (Exception e){
                    System.out.println(e);
                }
                //显示dialog

                //显示listview列表

            }
        });

        //创建订单

        submit_order_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否选择时间
                if (order_time_tv.getText().toString().equals("请选择时间")) {
                    Toast.makeText(context, "请选择送货时间", Toast.LENGTH_SHORT).show();
                }else {
                    //后台创建订单 删除购物车中 含有的订单商品
                    try {
                        RequestParams requestParams = new RequestParams(MainActivity.URL+"/order/createOrder");
                        String data=JsonUtil.ListToJson(payCommodityDTOS);
                        requestParams.addParameter("data",data);
                        requestParams.addParameter("uid",MainActivity.user_admin.getUid());
                        requestParams.addParameter("type_http",type_http);
                        x.http().post(requestParams, new Callback.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String result) {
                                Result r= null;
                                try {
                                    r = JsonUtil.JsonToObj(result, Result.class);
                                    if (r.getCode().equals("200")) {
                                        MainActivity.initUser();
                                        Intent intent =new Intent(getApplicationContext(),PayMoneyActivity.class);
                                        intent.putExtra("yingzhifu",yingzhifu_tv.getText().toString());
                                        intent.putExtra("order_number",r.getData().toString());
                                        startActivity(intent);
                                        finish();

                                    }else {
                                        Toast.makeText(context, "创建订单错误", Toast.LENGTH_SHORT).show();
                                    }

                                } catch (JsonProcessingException e) {
                                    e.printStackTrace();
                                }


                            }
                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {

                            }
                            @Override
                            public void onCancelled(CancelledException cex) {

                            }
                            @Override
                            public void onFinished() {

                            }
                        });
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }

                    // 更新数据



                }

                //

            }
        });

    }

    void getData() {
        String json_data_paycommoditys=getIntent().getStringExtra("json_data_pa");
        String all_p =getIntent().getStringExtra("all_price");
        all_pr_tv.setText(all_p);
        yingzhifu_tv.setText(all_p);
        daizhifu_tv.setText(all_p);

        try {
            payCommodityDTOS= JsonUtil.json2list(json_data_paycommoditys,PayCommodityDTO.class);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}